package com.yangguang.huahui.ui.activitys;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.yangguang.huahui.R;
import com.yangguang.huahui.entity.Attention;
import com.yangguang.huahui.ui.adapters.TypePageAdapter;
import com.yangguang.huahui.ui.fragments.AttentionFragment;
import com.yangguang.huahui.ui.fragments.BaseFragment;
import com.yangguang.huahui.ui.fragments.BaseMvpFragment;
import com.yangguang.huahui.ui.fragments.PublishBuyFragment;
import com.yangguang.huahui.ui.fragments.PublishSupplyFragment;
import com.yangguang.huahui.widget.WrapContentHeightViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PublishActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.mViewPager)
    ViewPager mViewPager;
    @BindView(R.id.mTabLayout)
    TabLayout mTabLayout;
    List<String> datas = new ArrayList<>();


    private List<BaseFragment> mFragments;
    private List<String> mTitles = new ArrayList<>();
    private TypePageAdapter mTypeAdapter;
    private PublishSupplyFragment supplyFragment;
    private PublishBuyFragment buyFragment;


    private int type;

    @OnClick({R.id.iv_back})
    void onClick(View v){
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }
    @Override
    protected int initLayoutId() {
        return R.layout.activity_publish;
    }

    @Override
    protected void initView() {
        mFragments = new ArrayList<>();
        tv_title.setText(getString(R.string.fabu_goods));
        mTabLayout.removeAllTabs();
        datas.add(getString(R.string.buy));
        datas.add(getString(R.string.supply));
        for (int i = 0; i < datas.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(datas.get(i)));
            mTitles.add(datas.get(i));
        }
        supplyFragment = new PublishSupplyFragment();
        buyFragment = new PublishBuyFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", getIntent().getIntExtra("type", 0));
        bundle.putString("id", getIntent().getStringExtra("id"));
        buyFragment.setArguments(bundle);
        supplyFragment.setArguments(bundle);
        mFragments.add(buyFragment);
        mFragments.add(supplyFragment);
        setPagerAdapter(mFragments, mTitles);
        mViewPager.setCurrentItem(getIntent().getIntExtra("position", 0));
    }

    @Override
    protected void initData() {

    }

    private void setPagerAdapter(List<BaseFragment> mFragments, List<String> mTitles){
        mTypeAdapter = new TypePageAdapter(getSupportFragmentManager());
        mTypeAdapter.setData(mFragments, mTitles);
        mViewPager.setAdapter(mTypeAdapter);
        mViewPager.setOffscreenPageLimit(mTitles.size() - 1);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabsFromPagerAdapter(mTypeAdapter);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        supplyFragment.onActivityResult(requestCode, resultCode, data);
    }
}
